package com.mail.anteater.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Config {

	final static Logger logger = Logger.getLogger(Config.class);
	private static Properties properties = null;
	public final static String CONFIG_FILE = "mail.conf";
	public final static String CONFIG_DIR = System.getProperty("mail.conf.dir");
	public final static String CONFIG_DELIMITER = ",";
	public static File configFile = null;
	public Config() {
		logger.info("Configuration server started.");
		init();
	}

	public String getString(String param) {
		String stringConfig = null;
		stringConfig = properties.getProperty(param);
		logger.info(param + "=" + stringConfig);
		return stringConfig;
	}

	public int getInteger(String param) {
		int intConfig;
		intConfig = Integer.parseInt(properties.getProperty(param));
		logger.info(param + "=" + intConfig);
		return intConfig;
	}

	public int getInteger(String param, int defaultValue) {
		int intConfig;
		try {
			intConfig = Integer.parseInt(properties.getProperty(param));
		} catch (NumberFormatException ex) {
			intConfig = defaultValue;
		}
		logger.info(param + "=" + intConfig);
		return intConfig;
	}

	public double getDouble(String param) {
		double doubleConfig;
		doubleConfig = Double.parseDouble(properties.getProperty(param));
		logger.info(param + "=" + doubleConfig);
		return doubleConfig;
	}

	public double getDouble(String param, double defaultValue) {
		double doubleConfig;
		try {
			doubleConfig = Double.parseDouble(properties.getProperty(param));
		} catch (NumberFormatException ex) {
			doubleConfig = defaultValue;
		}
		logger.info(param + "=" + doubleConfig);
		return doubleConfig;
	}

	public boolean getBoolean(String param) {
		boolean booleanConfig;
		booleanConfig = isTrue(properties.getProperty(param));
		logger.info(param + "=" + booleanConfig);
		return booleanConfig;
	}

	public ArrayList<String> getList(String param) {
		ArrayList<String> list = null;
		String ls = properties.getProperty(param);
		if (ls == null || "".equals(ls)) {
			return list;
		} else {
			list = new ArrayList<String>();
			for (String s : ls.split(CONFIG_DELIMITER)) {
				list.add(s);
			}
		}
		logger.info(param + "=" + list.toString());
		return list;
	}

	private boolean isTrue(String param) {
		if (param.equalsIgnoreCase("YES") || param.equalsIgnoreCase("1") || param.equalsIgnoreCase("TRUE") || param.equalsIgnoreCase("ON")
				|| param.equalsIgnoreCase("OK")) {
			return true;
		} else {
			return false;
		}
	}

	public void init() {

		File file = new File(CONFIG_DIR + "/" +CONFIG_FILE);
		String log4jConf = CONFIG_DIR + "/mail_log4j.properties";
		configFile = file;
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(file);
			properties = new Properties();
			properties.load(fis);
			PropertyConfigurator.configure(log4jConf);
		} catch (FileNotFoundException e) {
			System.out.println("Config file not found.");
			logger.error("Config file not found.", e);
		} catch (IOException e) {
			System.out.println("Config file reading error.");
			logger.error("Config file reading error.", e);
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					logger.error("Unable to close config input stream.", e);
				}
			}
		}
	}
	
	public void stop(){
		configFile = null;
	}
}
